﻿using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using JetBrains.Annotations;



namespace Crocodile.Debugging
{
    public static class Debugs
    {

        [AssertionMethod]
        public static void _assert_true_
            (
                [AssertionCondition(AssertionConditionType.IS_TRUE)] this bool value
            )
        {
            Debug.Assert(value);
        }


        [AssertionMethod]
        public static void _assert_true_
            (
                [AssertionCondition(AssertionConditionType.IS_TRUE)] this bool value, 
                string message = null
            )
        {
            Debug.Assert(value, message);
        }


        [AssertionMethod]
        public static void _assert_notnull_([AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this object obj, string message = null)
        {
            Debug.Assert(obj != null, message);
        }


        [AssertionMethod]
        public static void _assert_null_([AssertionCondition(AssertionConditionType.IS_NULL)] this object obj,string message = null)
        {
            Debug.Assert(obj == null, message);
        }


        [AssertionMethod]
        public static void _assert_nonempty_(this IEnumerable enumerable, string message = "Expected non-empty collection")
        {
            Debug.Assert(enumerable != null, message + " (but null)");
            Debug.Assert(enumerable.GetEnumerator().MoveNext(), message + " (but empty)");
        }


        [AssertionMethod]
        public static void _assert_positive_(this int value)
        {
            Debug.Assert(value > 0);
        }


        [AssertionMethod]
        public static void _assert_equals_<T>(this T @this, T that, string message = "Assertion failed")
        {
            bool ok = EqualityComparer<T>.Default.Equals(@this, that);
            if (!ok)
                Debug.Fail(message + ": got '"+@this.ToString()+"' when expected '"+that.ToString()+"'");
        }


        [AssertionMethod]
        public static void _assert_not_equals_<T>(this T @this, T that, string message = "Assertion failed")
        {
            bool ok = ! ( EqualityComparer<T>.Default.Equals(@this, that) );
            if (!ok)
                Debug.Fail(message + ": expected value not like '"+that.ToString()+"'");
        }



    }
}
